package Rainwater.controller;

import Rainwater.Service.UserService;
import Rainwater.baen.R;
import Rainwater.baen.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;

/**
 * @author cfy
 * @date 2024/10/16
 **/
@Controller
@RequestMapping("/rainwater/page")
public class UserController {
    @Autowired
    private UserService userService;
    private R r = new R();
    @GetMapping("/user")
    public ModelAndView pageIndex(@RequestParam(value = "pageNumber",required = false) Integer pageNumber){
        Integer upPage = 1;
        Integer downPage = 2;
        if (pageNumber==null||pageNumber<2){
            pageNumber = 1;
        }else {
            upPage = pageNumber-1;
            downPage = pageNumber +1;
        }
        ArrayList<UserVo> userVos = userService.getUserByPage(pageNumber);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("data_user");
        modelAndView.addObject("userVos",userVos);
        modelAndView.addObject("upPage",upPage);
        modelAndView.addObject("downPage",downPage);
        return modelAndView;
    }
    @GetMapping("/user/identity")
    public RedirectView UpdateIdentity(@RequestParam("id") Integer id){
        //执行代码
//        System.out.println("update"+id);
        userService.updateIdentity(id);
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("/rainwater/page/user");
        return redirectView;
    }
    @GetMapping("/user/delete")
    public RedirectView delateUser(@RequestParam("id") Integer id){
        //删除
//        System.out.println("delete"+id);
        userService.deleteUser(id);
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("/rainwater/page/user");
        return redirectView;
    }
}
